package org.projects.graduates.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.imageio.ImageIO;

import org.projects.graduates.Constants;

public class FileCommon {
	/**
	 * 保存图片
	 * 
	 * @param img
	 *            原图路径
	 * @param dest
	 *            目标图路径
	 * @param top
	 *            选择框的左边y坐标
	 * @param left
	 *            选择框的左边x坐标
	 * @param width
	 *            选择框宽度
	 * @param height
	 *            选择框高度
	 * @return
	 * @throws IOException
	 */
	public static boolean saveImage(File img, String dest, int top, int left, int width, int height) throws IOException {
		File fileDest = new File(dest);
		if (!fileDest.getParentFile().exists())
			fileDest.getParentFile().mkdirs();
		String ext = extOf(dest).toLowerCase();
		BufferedImage bi = (BufferedImage) ImageIO.read(img);
		height = Math.min(height, bi.getHeight());
		width = Math.min(width, bi.getWidth());
		if (height <= 0)
			height = bi.getHeight();
		if (width <= 0)
			width = bi.getWidth();
		top = Math.min(Math.max(0, top), bi.getHeight() - height);
		left = Math.min(Math.max(0, left), bi.getWidth() - width);

		BufferedImage bi_cropper = bi.getSubimage(left, top, width, height);
		return ImageIO.write(bi_cropper, ext.equals("png") ? "png" : "jpeg", fileDest);

	}

	/**
	 * 计算用户的文件存储位置
	 * 
	 * @param realPath
	 * @param personId
	 * @return
	 */
	public static String getPersonSpace(String realPath, long personId) {
		String result = realPath + File.separator + Constants.UPLOAD_DIR;
		result += File.separator + personId + File.separator;
		return result;
	}

	/**
	 * 返回用户头像的url地址
	 * 
	 * @param oppositePath
	 * @param personId
	 * @return
	 */
	public static String headUrl(String oppositePath, long personId) {
		String result = oppositePath + "/" + Constants.UPLOAD_DIR;
		result += "/" + personId + "/" + "head.jpg";
		return result;
	}

	/**
	 * 返回用户头像的物理地址
	 * 
	 * @param oppositePath
	 * @param personId
	 * @return
	 */
	public static String headPath(String realPath, long personId) {
		String result = realPath + File.separator + Constants.UPLOAD_DIR;
		result += File.separator + personId + File.separator + "head.jpg";
		return result;
	}

	public static void ifNotMk(String dir) {
		File uploadDir = new File(dir);
		if (!uploadDir.exists()) {
			uploadDir.mkdirs();
		}
	}

	/**
	 * 返回文件名的扩展名
	 * 
	 * @param fileName
	 * @return
	 */
	public static String extOf(String fileName) {
		return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
	}

	public static void write(File file, String fileName, String where) {
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(file);
			File uploadFile = new File(where, fileName);
			if (!uploadFile.getParentFile().exists()) {
				uploadFile.getParentFile().mkdirs();
			}
			os = new FileOutputStream(uploadFile);
			int length = 0;
			byte buffer[] = new byte[1024];
			while ((length = is.read(buffer)) != -1) {
				os.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		} finally {
			closeFileStream(is, os);
		}
	}
	  /**
     * 删除文件
     * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
     * @param fileContent String
     * @return boolean
     */
    public static void delFile(String filePathAndName) {
      try {
        String filePath = filePathAndName;
        filePath = filePath.toString();
        java.io.File myDelFile = new java.io.File(filePath);
        if(null != myDelFile &&myDelFile.exists()){
        	myDelFile.delete();
        }
      }
      catch (Exception e) {
        System.out.println("删除文件操作出错");
        e.printStackTrace();

      }

    }
	//新建文件
	public static void newFile(String filePathAndName, String fileContent) {

		try {
			String filePath = filePathAndName;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			if (!myFilePath.exists()) {
				myFilePath.createNewFile();
			}
			FileWriter resultFile = new FileWriter(myFilePath);
			PrintWriter myFile = new PrintWriter(resultFile);
			String strContent = fileContent;
			myFile.println(strContent);
			resultFile.close();

		} catch (Exception e) {
			System.out.println("新建文件操作出错 ");
			e.printStackTrace();
		}

	}

	private static void closeFileStream(InputStream is, OutputStream os) {
		try {
			if (os != null) {
				os.close();
			}
			if (is != null) {
				is.close();
			}
			os = null;
			is = null;
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

}
